package com.haois.mp.wechat.task;

import com.haois.mp.wechat.common.BusinessRemind;
import com.haois.mp.database.commom.constant.StatusCode;
import com.haois.mp.database.module.remote.service.impl.UserServiceImpl;
import com.haois.mp.database.module.common.dao.MpUserTokenMapper;
import com.haois.mp.database.module.wxserver.util.TempMsgUtil;
import com.haois.mp.database.util.mp.ProduceJwt;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * description
 *
 * @author RuiHaoZ on GuangZhou
 * @date 2018/8/5 21:15
 */
@Component
public class BusinessCheckTask {

    @Value("${defined.wxmp.sys-openid}")
    private String sysOpenId;
    @Autowired
    private MpUserTokenMapper mpUserTokenMapper;
    @Autowired
    private UserServiceImpl userService;

    ProduceJwt produceJwt = new ProduceJwt();

    Map<String, Object> params = new HashMap<>();

    //    @Scheduled(cron = "*/5 * * * * ?")
    @Scheduled(cron = "0 50 17 ? * *")
    public Map<String, Object> dayRemind() throws WxErrorException {
        long time1 = System.currentTimeMillis();
        boolean sysStatue = true;
        long time2 = 0;
        long time3 = 0;
        long time4 = 0;
        StringBuffer stringBuffer = new StringBuffer("");
        try {
            String access_token = produceJwt.getShareToken();
            Map result = userService.getUserInfo(access_token, "201430098009", StatusCode.SHARE_OID);
            String code = (String) result.get("code");
            time2 = System.currentTimeMillis();
            stringBuffer.append("系统数据服务端运行正常，花费 ").append(time2 - time1)
                    .append(" 毫秒，返回码[").append(code).append("]");
        } catch (Exception e) {
            sysStatue = false;
            stringBuffer.append("系统数据服务端运行异常，为：").append(e.getClass());
        }
        stringBuffer.append("\n");

        try {
            long count = mpUserTokenMapper.findWxUserTokenCount();
            time3 = System.currentTimeMillis();
            stringBuffer.append("微信端数据库运行正常，花费 ").append(time3 - time2)
                    .append(" 毫秒，用户数[").append(count).append("]");
        } catch (Exception e) {
            sysStatue = false;
            stringBuffer.append("微信端数据库运行异常，为：").append(e.getClass());
        }
        stringBuffer.append("\n");

        time4 = System.currentTimeMillis();
        stringBuffer.append("微信端系统服务运行平稳，检测共花费 ").append(time4 - time1).append(" 毫秒");

        BusinessRemind.remindByWx(new String[]{"定时检测", "", stringBuffer.toString()},
                sysOpenId, TempMsgUtil.title_Sys,
                TempMsgUtil.temp_Sys, TempMsgUtil.temp_Sys_Color);

        params.put("pushSize", 1);
        params.put("accordSize", 1);
        params.put("content", stringBuffer.toString());
        return params;
    }
}
